package org.hzz.application.handler.user;

import io.gitee.pkmer.convention.converter.TargetAndSourceConverter;
import io.gitee.pkmer.convention.page.PageResponse;
import io.gitee.pkmer.designpattern.strategy.AbstractExecuteStrategy;
import lombok.AllArgsConstructor;
import org.hzz.application.command.user.UserCouponPageCmd;
import org.hzz.promotions.domain.aggregate.CouponAggregate;
import org.hzz.promotions.domain.entity.CouponEntity;
import org.hzz.promotions.domain.service.CouponDomainService;
import org.hzz.promotions.domain.service.UserCouponDomainService;
import org.hzz.promotions.types.vo.UserCouponVo;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author <a href="mailto:1193094618@qq.com">pkmer</a>
 * <br>
 * <a href = "https://gitee.com/developeros/videos-online">Code Repository</a>
 * At 2024/9/4
 */
@Component
@AllArgsConstructor
public class UserCouponPageCmdHandler
        implements AbstractExecuteStrategy<UserCouponPageCmd, PageResponse<UserCouponVo>> {

    private final UserCouponDomainService userCouponDomainService;
    private final CouponDomainService couponDomainService;

    @Override
    public String mark() {
        return UserCouponPageCmd.MARK;
    }

    @Override
    public PageResponse<UserCouponVo> executeWithResp(UserCouponPageCmd cmd) {

        PageResponse<CouponAggregate> page = userCouponDomainService.pagePage(cmd.getPage());

        return new PageResponse<UserCouponVo>()
                .setCurrentPageNo(page.getCurrentPageNo())
                .setTotal(page.getTotal())
                .setTotalPages(page.getTotalPages())
                .setList(toVos(page.getList()));
    }

    private List<UserCouponVo> toVos(List<CouponAggregate> list){
        List<CouponEntity> coupons = list.stream().map(CouponAggregate::getCoupon).toList();
        return Converter.INSTANCE.toTargets(coupons);
    }

    @Mapper
    interface Converter extends TargetAndSourceConverter<UserCouponVo, CouponEntity> {
        Converter INSTANCE = Mappers.getMapper(Converter.class);
    }


 }
